package com.htzz.collection.service.impl;

import com.htzz.collection.bean.DevInfoEntity;
import com.htzz.collection.bean.KInfoEntity;
import com.htzz.collection.dao.DevInfoDao;
import com.htzz.collection.domain.DevInfoDto;
import com.htzz.collection.service.DevInfoService;
import com.htzz.collection.service.KInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("devInfoService")
public class DevInfoServiceImpl implements DevInfoService {

    @Autowired
    DevInfoDao devInfoDao;

    @Autowired
    KInfoService kInfoService;

    @Override
    public List<DevInfoEntity> seleceListByDto(DevInfoDto dto) {
        return devInfoDao.selectList(dto);
    }

    @Override
    public DevInfoEntity getById(Integer id) throws Exception {
        return devInfoDao.selectById(id);
    }

    @Override
    public DevInfoEntity getByDevId(String devId) throws Exception {
        return devInfoDao.selectByDevId(devId);
    }

    @Override
    @Transactional
    public String save(DevInfoEntity devInfo) throws Exception {
        //判断devId是否已经存在
        DevInfoEntity entity=devInfoDao.selectByDevId(devInfo.getDevId());

        if(entity!=null){
            throw new Exception("devId已经存在！");
        }

        //保存设备信息
        devInfoDao.insert(devInfo);
        //保存采集点信息
        List<KInfoEntity> kinfoList=devInfo.getKinfoList();
        if(kinfoList!=null&&kinfoList.size()>0){
            for (int i = 0; i < kinfoList.size(); i++) {
                KInfoEntity k=kinfoList.get(i);
                k.setDevId(devInfo.getDevId());
                kInfoService.save(k);
            }
        }

        return "保存成功";
    }

    @Override
    @Transactional
    public String updateById(DevInfoEntity devInfo)  throws Exception {
        int num=devInfoDao.updateById(devInfo);
        return "修改成功";
    }

    @Override
    @Transactional
    public String updateByDevId(DevInfoEntity devInfo)  throws Exception {
        int num=devInfoDao.updateByDevId(devInfo);
        return "修改成功";
    }

    @Override
    @Transactional
    public String removeByIds(List<String> asList)  throws Exception {
        if(asList!=null && asList.size()>0){
            for (int i = 0; i < asList.size(); i++) {
                devInfoDao.deleteByDevId(asList.get(i));
            }
        }
        return "删除成功";
    }
}
